//
//  HMStatusesCell.swift
//  Weibo
//
//  Created by 吴少飞 on 16/9/23.
//  Copyright © 2016年 wushaofei. All rights reserved.
//

import UIKit



class HMStatusesCell: UITableViewCell {

    //记录当前toolbar顶部约束
    var toolBarTopConstraint : Constraint?
    
    var  statusesViewModel : HMStatusesViewModel?{
        didSet{
            
           originaView.statusesViewModel = statusesViewModel
           toolBar.statusesViewModel = statusesViewModel
           
         toolBarTopConstraint?.uninstall()
         
            //  判断其是否有转发微博

            if statusesViewModel?.stuatus?.retweeted_status != nil {
                retweetView.isHidden = false
                toolBar.snp_updateConstraints(closure: { (make) in
                    toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                retweetView.statusViewModel = statusesViewModel
            }else{
                
                //  没有转发微博
                //  更新约束, 隐藏转发微博视图
                retweetView.isHidden = true
                toolBar.snp_updateConstraints(closure: { (make) in
                    
                    toolBarTopConstraint = make.top.equalTo(originaView.snp_bottom).constraint
                })
                
            }

            
            
        }
    }
    
    //  MARK: --懒加载添加控件
    //自己微博视图
    private lazy var originaView : HMStatusOriginalView = HMStatusOriginalView()
    private lazy var toolBar : HMStatusToolBarView = HMStatusToolBarView()
    //  转发微博视图
    private lazy var retweetView: HMStatusRetweetView = HMStatusRetweetView()

    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //  MARK: --给视图添加控件
    func setupUI() {
        
        contentView.addSubview(originaView)
        contentView.addSubview(toolBar)
        contentView.addSubview(retweetView)
        
        originaView.snp_makeConstraints { (make) in
            
            make.top.equalTo(contentView)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        retweetView.snp_makeConstraints { (make) in
            make.top.equalTo(originaView.snp_bottom)
            make.leading.equalTo(originaView)
            make.trailing.equalTo(originaView)
        }
        
        toolBar.snp_makeConstraints { (make) in
            toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.equalTo(retweetView)
            make.trailing.equalTo(retweetView)
            make.height.equalTo(35)
            
            make.bottom.equalTo(contentView.snp_bottom)
            
        }

        
    }
}












